import React, { PureComponent } from 'react';
import PropTypes from 'prop-types';
import { EventEmitter } from 'events'

// 事件总线：event bus
const eventBus = new EventEmitter();


class Home extends PureComponent {
  componentDidMount() {
    //增加事件监听
    eventBus.addListener("sayHello", this.handleSayHelloListener)
  }
  componentWillUnmount() {
    //取消事件监听
    eventBus.removeListener("sayHello", this.handleSayHelloListener)
  }

  handleSayHelloListener(message,num) {
    console.log(message);
    console.log(num);
  }

  render() {
    return (
      <div>
        Home
      </div>
    );
  }
}

class Profile extends PureComponent {
  render() {
    return (
      <div>
        Profile
        <button onClick={e => this.emmitEvent()}>点击了Profile按钮</button>
      </div>
    );
  }
  emmitEvent() {
    // eventBus.emit(事件名,参数1,参数2)
    eventBus.emit("sayHello", "Hello Home", 123)
  }
}

class App extends PureComponent {
  render() {
    return (
      <div>
        <Home></Home>
        <Profile></Profile>
      </div>
    );
  }
}

App.propTypes = {

};

export default App;